package com.app.twth.ui.tally.damagesku;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.ui.checkin.FileUpload;
import com.app.twth.ui.tally.TallyDetails;
import com.app.twth.ui.tally.sku.CheckSkuUnit;
import com.app.twth.ui.tally.sku.SkuInfo;
import com.app.twth.ui.tally.sku.SkuInfoQr;
import com.app.twth.utils.DateUtil;
import com.lzy.okgo.model.Response;

import java.util.List;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class DamageSkuViewModel extends BaseAndroidViewModel<DamageSkuDataModel> {

    public MutableLiveData<List<TallyDetails>> tallyDetailsLiveData = new MutableLiveData<>();
    public MutableLiveData<Integer> tallySkuDetailsLiveData = new MutableLiveData<>();
    public MutableLiveData<SkuInfo> tallySkuInfoLiveData = new MutableLiveData<>();
    public MutableLiveData<CheckSkuUnit> checkSkuUnitLiveData = new MutableLiveData<>();
    public MutableLiveData<FileUpload> fileUploadMutableLiveData = new MutableLiveData<>();


    public DamageSkuViewModel(@NonNull Application application) {
        super(application);
    }

    public void getTallySkuInfo(SkuInfoQr skuInfoQr, boolean isSubmit) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.<BaseResponseEntity<SkuInfo>>getTallySkuInfo(skuInfoQr, new JsonCallBack<BaseResponseEntity<SkuInfo>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<SkuInfo>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<SkuInfo> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    SkuInfo data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    data.setSubmit(isSubmit);
                    tallySkuInfoLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }


    public void fileUpload(String containerNo, String fileName, String path, int type, int position, boolean isSubmit, boolean showDialog) {
        isLoading.setValue(new LoadingBean(showDialog, Constant.loading));
        errorResultCode.setValue("");
        String name = "tally/" + containerNo + "-" + fileName + "-" + DateUtil.getYMD() + "-" + System.currentTimeMillis() + ".jpg";
        mModel.<Object>upload(name, path, new JsonCallBack<BaseResponseEntity<String>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<String>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<String> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    FileUpload fileUpload = new FileUpload();
                    fileUpload.setType(type);
                    fileUpload.setPosition(position);
                    fileUpload.setPath(baseResponseEntity.data);
                    fileUpload.setSubmit(isSubmit);
                    fileUploadMutableLiveData.setValue(fileUpload);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(baseResponseEntity.getCode());
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(error);
            }
        });
    }

}
